﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;
using System.IO;

namespace WebBanThuoc.admin
{
    public partial class QLAnhSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }
            else
            {
                string p_masanpham = Request.QueryString["p_masanpham"];
                if (p_masanpham != null)
                {
                    IServerWeb sw = new ServerWeb();
                    getDataSanPham(p_masanpham.Replace("SP", "").Trim(), sw);
                }
            }



            if (!IsPostBack)
            {

            }
            else
            {
                if (checkDefaultSubmit())
                {
                    string MaSanPham = Request.Params["MaSanPham"];
                    if (MaSanPham != null && MaSanPham.Trim() != "")
                    {
                        IServerWeb sw = new ServerWeb();
                        getDataSanPham(MaSanPham, sw);
                    }
                }
                else
                {
                    if (Request.Params["btn_Them"] != null)
                    {
                        addPicture();
                    }                    
                    if (Request.Params["btn_Xoa"] != null)
                    {
                        deletePicture();
                    }
                }
            }
        }

        private void getDataSanPham(string MaSanPham, IServerWeb sw)
        {
            SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
            SanPham sp = daoSP.getByKey("SP" + MaSanPham);
            if (sp != null)
            {
                HinhAnh_DAO daoHA = new HinhAnh_DAO(sw.Connection);
                List<DTO.HinhAnh> listHA = daoHA.getListByKey(sp.MaSanPham);
                Session.Add("page_QLAnhSanPham_dataSP", sp);
                Session.Add("page_QLAnhSanPham_dataHinhAnh", listHA);
                if (listHA.Count <= 0)
                {
                    Session.Add("page_QLAnhSanPham_msgSucessFunc", "Sản phẩm này chưa có hình ảnh.");
                }
                sw.closeConnection();
            }
            else
            {

            }
        }
        private bool checkDefaultSubmit()
        {
            if (Request.Params["btn_Xoa"] != null || Request.Params["btn_Them"] != null)
            {
                return false;
            }
            return true;
        }

        private void addPicture()
        {
            string str_UploadFolder = System.Configuration.ConfigurationManager.AppSettings["AnhSanPham"];
            string str_FolderPath_Server = Server.MapPath(str_UploadFolder);
            int maxSizeFile = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["KichThuocFileAnh"]);
            string str_FileName = "";
            string MaSanPham = Request.Params["MaSanPham"];
            IServerWeb sw = new ServerWeb();
            HinhAnh_DAO daoHA = new HinhAnh_DAO(sw.Connection);

            if(MaSanPham == null || MaSanPham == "")
            {
                return;
            }

            if (!Directory.Exists(str_FolderPath_Server))
            {
                Directory.CreateDirectory(str_FolderPath_Server);
            }
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFile file = Request.Files[i];
                if (file.ContentLength <= maxSizeFile && Func.isFileImage(file.ContentType))
                {
                    DTO.HinhAnh objHA = new DTO.HinhAnh();
                    objHA.IDHinhAnh = daoHA.createIDHinhAnh();
                    str_FileName = objHA.IDHinhAnh.ToString() + "." + Func.getFileTypeImage(file.ContentType);
                    string pathSaveFile = str_FolderPath_Server + "/" + str_FileName;

                    file.SaveAs(pathSaveFile);
                    if (File.Exists(pathSaveFile))
                    {
                        objHA.DuongDan = str_UploadFolder + "/" + str_FileName;
                        objHA.MaSanPham = "SP" + MaSanPham;
                        objHA.ChuThich = "";
                        daoHA.add(objHA);                        
                    }
                }
            }
            getDataSanPham(MaSanPham, sw);
        }

        private void deletePicture()
        {
            //string str_UploadFolder = System.Configuration.ConfigurationManager.AppSettings["AnhSanPham"];
            //string str_FolderPath_Server = Server.MapPath(str_UploadFolder);
            string pathSaveFile = "";            
            string MaSanPham = Request.Params["MaSanPham"];
            string[] listIDHinhAnh;
            if (Request.Params["IDHinhAnh"] != null)
            {
                listIDHinhAnh = Request.Params["IDHinhAnh"].Split(',');

                IServerWeb sw = new ServerWeb();
                HinhAnh_DAO daoHA = new HinhAnh_DAO(sw.Connection);
                DTO.HinhAnh objHA;
                for (int i = 0; i < listIDHinhAnh.Length; i++)
                {
                    objHA = daoHA.getByKey(int.Parse(listIDHinhAnh[i]));
                    pathSaveFile = Server.MapPath(objHA.DuongDan);
                    File.Delete(pathSaveFile);
                    if (!File.Exists(pathSaveFile))
                    {
                        daoHA.deleteByKey(objHA.IDHinhAnh);

                    }
                }

                getDataSanPham(MaSanPham, sw);
            }

            
        }
    }
}